<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class m_member extends CI_Model {

    var $member_id;
    var $member_nama;
    var $member_pass;
    var $member_namanya;
    var $member_kelamin;
    var $member_email;
    var $member_role;
    var $member_aktifasi;
    var $member_status;
    var $member_tanggal;
    var $member_oleh;
    var $member_dirubah;
    var $cari;
    var $paginate;
    var $num_pages;
    var $offset;

    function __construct() {
        parent::__construct();
    }

    public function get_row_all() {
        if ($this->cari) {
            $this->db->where('(member_nama LIKE ' . $this->db->escape('%' . $this->cari . '%') . 'OR member_namanya LIKE ' . $this->db->escape('%' . $this->cari . '%') . 'OR member_email LIKE ' . $this->db->escape('%' . $this->cari . '%') . ')');
        }

        $query = $this->db->count_all_results('t_member');

        return $query;
    }

    public function get() {
        if ($this->cari) {
            $this->db->where('(member_nama LIKE ' . $this->db->escape('%' . $this->cari . '%') . 'OR member_namanya LIKE ' . $this->db->escape('%' . $this->cari . '%') . 'OR member_email LIKE ' . $this->db->escape('%' . $this->cari . '%') . ')');
        }

        if ($this->paginate == TRUE) {
            $query = $this->db->get('t_member', $this->num_pages, $this->offset);
            return $query;
        }

        $query = $this->db->get('t_member');
        return $query;
    }

    public function get_by_id($id = '0') {
        $this->db->where('member_id', $id);
        $query = $this->db->get('t_member');

        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return redirect('homes/');
        }
    }

    public function get_member_name($name) {
        $this->db->select('member_nama');
        $this->db->where('member_nama', $name);
        $query = $this->db->get('t_member');

        if ($query->num_rows()) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function get_member_namenya($id) {
        $this->db->select('member_namanya');
        $this->db->where('member_id', $id);
        $query = $this->db->get('t_member');

        if ($query->num_rows() > 0) {
            return $query->row()->member_namanya;
        } else {
            return '';
        }
    }

    public function save() {
        if ($this->member_email) {
            $db_array['member_email'] = $this->member_email;
        }

        if ($this->member_namanya) {
            $db_array['member_namanya'] = $this->member_namanya;
        }
        if ($this->member_kelamin) {
            $db_array['member_kelamin'] = $this->member_kelamin;
        }

        if ($this->member_status) {
            $db_array['member_status'] = $this->member_status;
        }

        if ($this->member_role) {
            $db_array['member_role'] = $this->member_role;
        }

        if ($this->member_nama) {
            $db_array['member_nama'] = $this->member_nama;
        }

        if ($this->member_pass) {
            $db_array['member_pass'] = md5($this->member_pass);
        }

        if ($this->member_aktifasi) {
            $db_array['member_aktifasi'] = $this->member_aktifasi;
        }

        if ($this->member_tanggal) {
            $db_array['member_tanggal'] = $this->member_tanggal;
        }

        if ($this->member_oleh) {
            $db_array['member_oleh'] = $this->member_oleh;
        }

        if ($this->member_dirubah) {
            $db_array['member_dirubah'] = $this->member_dirubah;
        }

        if ($this->member_id) {
            $this->db->where('member_id', $this->member_id);
            $this->db->update('t_member', $db_array);
        } else {
            $this->db->insert('t_member', $db_array);
        }
    }

    public function delete() {
        $this->db->where('member_id', $this->member_id);
        $this->db->delete('t_member');
    }

    public function get_member_menu() {
        $this->db->select('member_id, member_namanya');
        $this->db->order_by('member_id', 'ASC');
        $query = $this->db->get('t_member');
        $result = $query->result();

        $kategori_menu = array();
        $pilihan[''] = 'Pilih';

        foreach ($result AS $row) {
            $pilihan[$row->member_id] = $row->member_namanya;
        }
        return $pilihan;
    }

    public function to_exel() {
        $this->load->dbutil();
        $delimiter = "\t|";
        $newline = "\r\n";
        $enclosure = "";

        $this->db->select('member_nama AS "Nama Pengguna"');
        $this->db->select('member_namanya AS "Nama Lengkap"');
        $this->db->select('member_email AS "Email"');
        $query = $this->db->get('t_member');

        return $this->dbutil->csv_from_result($query, $delimiter, $newline, $enclosure);
    }

    public function set_aktifasi() {
        if ($this->member_id AND ($this->member_aktifasi == 0 OR $this->member_aktifasi == 1)) {
            $oleh = $this->get_member_namenya($this->session->userdata('member_id'));

            $this->db->where('member_id', $this->member_id);
            $db_array = array('member_aktifasi' => $this->member_aktifasi,
                'member_oleh' => $oleh,
                'member_dirubah' => date('Y-m-d H:i:s'));
            $this->db->update('t_member', $db_array);
        }
    }

}

/* End of file m_member.php */
/* Location: ./application/model/m_member.php */